package com.ruoyi.controller.inventory;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.inventory.MesWarehouse;
import com.ruoyi.service.inventory.IMesWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/inventory/warehouse")
public class MesWarehouseController extends BaseController
{
    @Autowired
    private IMesWarehouseService IMesWarehouseService;

    /**
     * 查询仓库信息列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesWarehouse mesWarehouse)
    {
        startPage();
        List<MesWarehouse> list = IMesWarehouseService.selectMesWarehouseList(mesWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库信息列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:warehouse:export')")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWarehouse mesWarehouse)
    {
        List<MesWarehouse> list = IMesWarehouseService.selectMesWarehouseList(mesWarehouse);
        ExcelUtil<MesWarehouse> util = new ExcelUtil<MesWarehouse>(MesWarehouse.class);
        util.exportExcel(response, list, "仓库信息数据");
    }

    /**
     * 获取仓库信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:warehouse:query')")
    @GetMapping(value = "/{warehouseId}")
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(IMesWarehouseService.selectMesWarehouseByWarehouseId(warehouseId));
    }

    /**
     * 新增仓库信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:warehouse:add')")
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesWarehouse mesWarehouse)
    {
        // 校验仓库编码唯一性
        if ("1".equals(IMesWarehouseService.checkWarehouseCodeUnique(mesWarehouse.getWarehouseCode())))
        {
            return error("新增仓库失败，仓库编码已存在");
        }
        return toAjax(IMesWarehouseService.insertMesWarehouse(mesWarehouse));
    }

    /**
     * 修改仓库信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:warehouse:edit')")
    @Log(title = "仓库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesWarehouse mesWarehouse)
    {
        // 校验仓库编码唯一性（排除自身）
        MesWarehouse existWarehouse = IMesWarehouseService.selectMesWarehouseByWarehouseId(mesWarehouse.getWarehouseId());
        if (existWarehouse != null && !existWarehouse.getWarehouseCode().equals(mesWarehouse.getWarehouseCode()))
        {
            if ("1".equals(IMesWarehouseService.checkWarehouseCodeUnique(mesWarehouse.getWarehouseCode())))
            {
                return error("修改仓库失败，仓库编码已存在");
            }
        }
        return toAjax(IMesWarehouseService.updateMesWarehouse(mesWarehouse));
    }

    /**
     * 删除仓库信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:warehouse:remove')")
    @Log(title = "仓库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds)
    {
        return toAjax(IMesWarehouseService.deleteMesWarehouseByWarehouseIds(warehouseIds));
    }

    /**
     * 校验仓库编码
     */
    @PostMapping("/checkWarehouseCodeUnique")
    public AjaxResult checkWarehouseCodeUnique(@RequestBody MesWarehouse mesWarehouse)
    {
        String result = IMesWarehouseService.checkWarehouseCodeUnique(mesWarehouse.getWarehouseCode());
        if ("1".equals(result))
        {
            return error("仓库编码已存在");
        }
        return success();
    }

    /**
     * 根据仓库类型查询仓库列表
     */
    @GetMapping("/listByType/{warehouseType}")
    public AjaxResult listByType(@PathVariable String warehouseType)
    {
        List<MesWarehouse> list = IMesWarehouseService.selectWarehousesByType(warehouseType);
        return success(list);
    }

    /**
     * 查询启用状态的仓库列表
     */
    @GetMapping("/listActive")
    public AjaxResult listActive()
    {
        List<MesWarehouse> list = IMesWarehouseService.selectActiveWarehouses();
        return success(list);
    }
}